package leetcode.offer2069;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Offer2069 implements IDebug {

	public int peakIndexInMountainArray(int[] arr) {
		int l = 0;
		int r = arr.length - 1;
		while (l < r) {
			int mid = ((r - l) >> 1) + l;
			if (mid == 0)
				return r;
			if (arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1]) {
				return mid;
			} else if (arr[mid] < arr[mid - 1]) {
				r = mid - 1;
			} else if (arr[mid] < arr[mid + 1]) {
				l = mid + 1;
			}
		}
		return l;
	}

	@Override
	public void debug1() {
		String str = "[0,1,0]";
		int e = 1;
		int r = this.peakIndexInMountainArray(HelpDebug.str2array3(str));
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug2() {
		String str = "[1,3,5,4,2]";
		int e = 2;
		int r = this.peakIndexInMountainArray(HelpDebug.str2array3(str));
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug3() {
		String str = "[0,10,5,2]";
		int e = 1;
		int r = this.peakIndexInMountainArray(HelpDebug.str2array3(str));
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug4() {
		String str = "[3,4,5,1]";
		int e = 2;
		int r = this.peakIndexInMountainArray(HelpDebug.str2array3(str));
		HelpDebug.compare(e, r);
	}

	public void debug5() {
		String str = "[24,69,100,99,98,97,79,78,67,36,26,19]";
		int e = 2;
		int r = this.peakIndexInMountainArray(HelpDebug.str2array3(str));
		HelpDebug.compare(e, r);
	}

	public void debug6() {
		String str = "[18,29,38,59,98,100,99,98,90]";
		int e = 5;
		int r = this.peakIndexInMountainArray(HelpDebug.str2array3(str));
		HelpDebug.compare(e, r);
	}

	public void debug7() {
		String str = "[3,5,3,2,0]";
		int e = 1;
		int r = this.peakIndexInMountainArray(HelpDebug.str2array3(str));
		HelpDebug.compare(e, r);
	}

	public static void main(String[] args) {
		Offer2069 so = new Offer2069();
		so.debug7();
		so.debug6();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		so.debug5();
	}

}
